package org.jeecg.modules.system.service.impl;

import java.util.*;

import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.mapper.SysRoleMapper;
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @Author scott
 * @since 2018-12-21
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Resource
    SysUserRoleMapper sysUserRoleMapper;
    @Resource
    SysRoleMapper sysRoleMapper;


    @Override
    public List<Map<String, Object>> getUserRoleList(String userId) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<SysUserRole> userRoleList = sysUserRoleMapper.selectList(QueryGenerator.initQueryWrapper(new SysUserRole(),null)
                .lambda().eq(SysUserRole::getUserId,userId));
        if(oConvertUtils.listIsNotEmpty(userRoleList)){
            for (SysUserRole sysUserRole : userRoleList) {
                SysRole sysRole = sysRoleMapper.selectById(sysUserRole.getRoleId());
                if(oConvertUtils.isNotEmpty(sysRole)){
                    Map<String, Object> map = new HashMap<>();
                    map.put("roleName",sysRole.getRoleName());
                    map.put("value",sysRole.getRoleCode());
                    list.add(map);
                }
            }
        }
        return list;
    }
}
